1
|
|
|
import { isArray, isValue } from './checkType'; |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* transforms any value to Array |
5
|
|
|
* @param {any} value any array or primitive for examination |
6
|
|
|
* @returns {array} [] if no value passed, value if it is Array and \[value] in other cases |
7
|
|
|
*/ |
8
|
|
|
export function toArray(value) { |
9
|
|
|
if (!isValue(value)) return []; |
|
|
|
|
10
|
|
|
|
11
|
|
|
return isArray(value) ? value : [ value ]; |
12
|
|
|
} |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* generates filter function, that leaves only unique items |
16
|
|
|
* @param {function} isEqual equality function which receives 2 elements to compare, should return true if elements are equal |
17
|
|
|
* @returns {function} filter function, that leaves only unique items |
18
|
|
|
*/ |
19
|
|
|
export function uniqueFilter(isEqual) { |
20
|
|
|
return function filter(item, index, array) { |
21
|
|
|
const occurrence = isEqual |
22
|
|
|
? array.findIndex((i) => isEqual(item, i)) |
23
|
|
|
: array.indexOf(item); |
24
|
|
|
|
25
|
|
|
return occurrence === index; |
26
|
|
|
}; |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
filter function, that leaves only unique items with same id property |
31
|
|
|
@constant |
32
|
|
|
@type {function} |
33
|
|
|
*/ |
34
|
|
|
export const uniqueIdFilter = uniqueFilter((a, b) => a.id === b.id); |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
filter function, that leaves only identicly unique items (same by ===) |
38
|
|
|
@constant |
39
|
|
|
@type {function} |
40
|
|
|
*/ |
41
|
|
|
export const uniqueIdenticFilter = uniqueFilter((a, b) => a === b); |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
filter function, that leaves only exited values |
45
|
|
|
@constant |
46
|
|
|
@type {function} |
47
|
|
|
*/ |
48
|
|
|
export const existanceFilter = i => isValue(i); |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
filter function, that leaves all items |
52
|
|
|
@constant |
53
|
|
|
@type {function} |
54
|
|
|
*/ |
55
|
|
|
export const passFilter = () => true; |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* flattens array |
59
|
|
|
* @param {array} any array with nested arrays |
60
|
|
|
* @returns {array} flattened array |
61
|
|
|
*/ |
62
|
|
|
export function flatten(arr) { |
63
|
|
|
// eslint-disable-next-line unicorn/no-array-reduce |
64
|
|
|
return toArray(arr).reduce((flat, toFlatten) => { |
65
|
|
|
// eslint-disable-next-line unicorn/prefer-spread |
66
|
|
|
return flat.concat(Array.isArray(toFlatten) ? flatten(toFlatten) : toFlatten); |
67
|
|
|
}, []); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* checks are all items of array unique |
72
|
|
|
* @param {array} any array |
73
|
|
|
* @param {object} settings method configuration |
74
|
|
|
* @param {string} settings.field property to treat as unique value |
75
|
|
|
* @param {boolean} settings.ignoreEmpty if set, empty values will be ignored |
76
|
|
|
* @returns {array} flattened array |
77
|
|
|
*/ |
78
|
|
|
export function isUnique(array, { field, ignoreEmpty } = {}) { |
79
|
|
|
const map = new Map(); |
80
|
|
|
|
81
|
|
|
for (const item of array) { |
82
|
|
|
const value = !ignoreEmpty && field ? item[field] : item; |
83
|
|
|
|
84
|
|
|
if (ignoreEmpty && (value === undefined || value === null)) continue; |
|
|
|
|
85
|
|
|
if (map.has(value)) return false; |
|
|
|
|
86
|
|
|
map.set(value); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
return true; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* get last element of an array |
94
|
|
|
* @param {array} any array |
95
|
|
|
* @returns {any} last array element |
96
|
|
|
*/ |
97
|
|
|
export function last(array) { |
98
|
|
|
return array[array.length - 1]; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
export function groupBy(array, similar) { |
102
|
|
|
const groupFields = toArray(similar); |
103
|
|
|
const groups = {}; |
104
|
|
|
|
105
|
|
|
for (const item of array) { |
106
|
|
|
const uniqueValue = groupFields.map(key => item[key]).join('.'); |
107
|
|
|
|
108
|
|
|
if (!groups[uniqueValue]) groups[uniqueValue] = []; |
|
|
|
|
109
|
|
|
groups[uniqueValue].push(item); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
return groups; |
113
|
|
|
} |
114
|
|
|
|
Consider adding curly braces around all statements when they are executed conditionally. This is optional if there is only one statement, but leaving them out can lead to unexpected behaviour if another statement is added later.
Consider:
If you or someone else later decides to put another statement in, only the first statement will be executed.
In this case the statement
b = 42
will always be executed, while the logging statement will be executed conditionally.ensures that the proper code will be executed conditionally no matter how many statements are added or removed.